package main

import (
	"fmt"

	yaml "github.com/asim/go-micro/plugins/config/encoder/yaml/v3"
	"github.com/asim/go-micro/v3/config"
	"github.com/asim/go-micro/v3/config/reader"
	"github.com/asim/go-micro/v3/config/reader/json"
	"github.com/asim/go-micro/v3/config/source/file"
)

type Config struct {
	RedisAddress string `json:"redis_address"`
	Password     string `json:"redis_password"`
	MaxConn      int    `json:"maxconn"`
	EtcdUrls     string `json:"etcd_urls"`
	WsPort       int    `json:"ws_port"`
	GrpcPort     int    `json:"grpc_port"`
	WebPort      int    `json:"web_port"`
	Http2Port    int    `json:"http2_port"`
}

func CreateConfig() (Config, error) {
	// new yaml encoder
	enc := yaml.NewEncoder()

	// new config
	c, _ := config.NewConfig(
		config.WithReader(
			json.NewReader( // json reader for internal config merge
				reader.WithEncoder(enc),
			),
		),
	)

	var configs Config

	// load the config from a file source
	if err := c.Load(file.NewSource(
		file.WithPath("/smebxyz/golang/websocket-cluster/internal/handlerwebsocket/config.yaml"),
	)); err != nil {
		fmt.Println(err)
		return configs, err
	}

	// fmt.Println("data", c.Map())

	if err := c.Get("hosts").Scan(&configs); err != nil {
		fmt.Println(err)
		return configs, err
	} else {
		return configs, nil
	}
}
